package com.viphuli.fragment;

import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.Bind;
import butterknife.OnClick;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.loopj.android.http.RequestParams;
import com.offroader.share.wxapi.ShareUtils;
import com.offroader.utils.JsonUtils;
import com.offroader.utils.StringUtils;
import com.viphuli.app.R;
import com.viphuli.common.AccessTokenKeeper;
import com.viphuli.common.Constants;
import com.viphuli.common.Constants.ActionType;
import com.viphuli.common.Constants.ShareType;
import com.viphuli.common.MyApp;
import com.viphuli.common.MyPageHelper;
import com.viphuli.http.ApiRequest;
import com.viphuli.http.bean.part.HomeTopSlide;
import com.viphuli.http.handler.HomeResponseHandler;
import com.viphuli.http.handler.MyBaseHttpResponseHandler.HandlerType;

public class HomeFragment extends BaseProgressFragment implements BaseSliderView.OnSliderClickListener {

	@Bind(R.id.id_main_image_slider)
	protected SliderLayout slider;

	@Bind(R.id.id_main_content_service_item_health_icon)
	protected ImageView healthIcon;

	@Bind(R.id.id_main_content_service_item_health_title)
	protected TextView healthTitle;

	@Bind(R.id.id_main_content_service_item_health_content)
	protected TextView healthContent;

	@Bind(R.id.id_main_content_service_item_nurse_icon)
	protected ImageView nurseIcon;

	@Bind(R.id.id_main_content_service_item_nurse_title)
	protected TextView nurseTitle;

	@Bind(R.id.id_main_content_service_item_nurse_content)
	protected TextView nurseContent;

	@Bind(R.id.id_main_content_service_item_private_custom_icon)
	protected ImageView privateIcon;

	@Bind(R.id.id_main_content_service_item_private_custom_title)
	protected TextView privateTitle;

	@Bind(R.id.id_main_content_service_item_company_order_icon)
	protected ImageView companyIcon;

	@Bind(R.id.id_main_content_service_item_company_order_title)
	protected TextView companyTitle;

	@Override
	protected int obtainContentRes() {
		return R.layout.frag_home;
	}

	@Override
	protected void initParams() {
	}

	@Override
	protected void initView() {

		//标题
		caller.setTitle(getString(R.string.app_name));

		//顶部轮转图
		slider.setPresetTransformer(SliderLayout.Transformer.Default);
		slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
//		slider.setCustomAnimation(new DescriptionAnimation());

	}

	@Override
	protected void initData() {

		if (!isAdded())
			return;

		//网络请求
		initRequest(HandlerType.loading);

	}

	@Override
	protected void initRequest(HandlerType handlerType) {
		super.initRequest(handlerType);

		AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(MyApp.getInstance());

		//网络请求
		RequestParams params = new RequestParams();
		params.put("uid", tokenKeeper.getOpenId());
		ApiRequest.home.request(this, params, handlerType);
	}

	@Override
	public void onSliderClick(BaseSliderView slider) {
		if (!isAdded())
			return;

		//解析参数
		String slideJson = slider.getBundle().getString(HomeResponseHandler.SLIDE_KEY);

		//非法不处理
		if (StringUtils.isBlank(slideJson))
			return;

		//反序列化bean
		HomeTopSlide homeTopSlide = JsonUtils.fromJson(slideJson, HomeTopSlide.class);

		//需要登陆处理
		if (homeTopSlide.isNeedLogin()) {

			AccessTokenKeeper tokenKeeper = AccessTokenKeeper.readAccessToken(MyApp.getInstance());

			if (!tokenKeeper.isLogin()) {
				AccountLoginFragment.goWithConfirmResult(getActivity(), getString(R.string.tip_need_login));
				return;//返回不继续处理（登陆成功后刷新当前页面，状态变为已登陆，可继续执行）
			}

		}

		//业务处理
		if (ActionType.none.getType() == homeTopSlide.getActionType()) {//无
			//什么也不做
		} else if ((ActionType.webview.getType() == homeTopSlide.getActionType())) {//打开网页

			String title = homeTopSlide.getTitle();
			String url = homeTopSlide.getActionTypeParams().getWebview().getUrl();

			MyPageHelper.webView.showMyWebViewPage(getActivity(), url, title);

		} else if ((ActionType.nurseJoin.getType() == homeTopSlide.getActionType())) {//护士加入

			Bundle args = new Bundle();
			MyPageHelper.accountNurseJoinReady.showMyPage(getActivity(), args);

		} else if ((ActionType.privateCustom.getType() == homeTopSlide.getActionType())) {//私人定制

			MyPageHelper.homePrivateCustom.showMyPage(getActivity());

		} else if ((ActionType.serviceList.getType() == homeTopSlide.getActionType())) {//服务列表 

			int serviceType = homeTopSlide.getActionTypeParams().getServiceList().getType();

			Bundle args = new Bundle();
			args.putInt("service_type", serviceType);

			switch (serviceType) {
			case Constants.HOME_SERVICE_HEALTH_COMPANY:

				MyPageHelper.homeServiceHealth.showMyPage(getActivity(), args);

				break;

			case Constants.HOME_SERVICE_NURSE_PERSON:

				MyPageHelper.homeServiceNurse.showMyPage(getActivity(), args);

				break;

			case Constants.HOME_SERVICE_HEALTH_PERSON:

				MyPageHelper.homeServiceHealth.showMyPage(getActivity(), args);

				break;

			default:
				break;
			}

		} else if ((ActionType.share.getType() == homeTopSlide.getActionType())) {//分享

			String appId = MyApp.getInstance().getString(R.string.weichat_app_id);
			String appSecret = MyApp.getInstance().getString(R.string.weichat_app_secret);

			int platform = homeTopSlide.getActionTypeParams().getShare().getPlatform();
			String title = homeTopSlide.getActionTypeParams().getShare().getTitle();
			String content = homeTopSlide.getActionTypeParams().getShare().getContent();
			String imgUrl = homeTopSlide.getActionTypeParams().getShare().getImgUrl();
			String url = homeTopSlide.getActionTypeParams().getShare().getUrl();

			if (ShareType.weichat.getPlatform() == platform)
				ShareUtils.shareToWeiChat(getActivity(), appId, appSecret, title, content, imgUrl, url);
			else if (ShareType.weichat_circle.getPlatform() == platform)
				ShareUtils.shareToWeiChatCircle(getActivity(), appId, appSecret, title, content, imgUrl, url);

		}

	}

	/**
	 * 上门体检
	 */
	@OnClick(R.id.id_main_content_service_item_health_layout)
	public void goToServiceHealth() {

		Bundle args = new Bundle();
		args.putInt("service_type", Constants.HOME_SERVICE_HEALTH_PERSON);
		MyPageHelper.homeServiceHealth.showMyPage(getActivity(), args);

	}

	/**
	 * 上门护理 
	 */
	@OnClick(R.id.id_main_content_service_item_nurse_layout)
	public void goToServiceNurse() {

		Bundle args = new Bundle();
		args.putInt("service_type", Constants.HOME_SERVICE_NURSE_PERSON);
		MyPageHelper.homeServiceNurse.showMyPage(getActivity(), args);

	}

	/**
	 * 私人定制
	 */
	@OnClick(R.id.id_main_content_service_item_private_custom_layout)
	public void goToPrivateCustom() {

		MyPageHelper.homePrivateCustom.showMyPage(getActivity());

	}

	/**
	 * 企业预约
	 */
	@OnClick(R.id.id_main_content_service_item_company_order_layout)
	public void goToCompanyOrder() {

		//		MyPageHelper.homeCompanyOrder.showMyPage(getActivity());

		Bundle args = new Bundle();
		args.putInt("service_type", Constants.HOME_SERVICE_HEALTH_COMPANY);
		MyPageHelper.homeServiceHealth.showMyPage(getActivity(), args, getString(R.string.str_main_content_service_item_health_company));

	}

	@Override
	public void onBackPressed() {
		//balk
	}

	@Override
	public void onStart() {
		super.onStart();

		if (slider != null)
			slider.startAutoCycle();
	}

	@Override
	public void onStop() {
		super.onStop();

		if (slider != null)
			slider.stopAutoCycle();
	}

	public SliderLayout getSlider() {
		return slider;
	}

	public ImageView getHealthIcon() {
		return healthIcon;
	}

	public TextView getHealthTitle() {
		return healthTitle;
	}

	public TextView getHealthContent() {
		return healthContent;
	}

	public ImageView getNurseIcon() {
		return nurseIcon;
	}

	public TextView getNurseTitle() {
		return nurseTitle;
	}

	public TextView getNurseContent() {
		return nurseContent;
	}

	public ImageView getPrivateIcon() {
		return privateIcon;
	}

	public TextView getPrivateTitle() {
		return privateTitle;
	}

	public ImageView getCompanyIcon() {
		return companyIcon;
	}

	public TextView getCompanyTitle() {
		return companyTitle;
	}

}
